package leetcode.code0969;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	public List<Integer> pancakeSort(int[] arr) {
		List<Integer> ans = new ArrayList<>();
		int len = arr.length;
		int[] sort = new int[len];
		for (int i = 0; i < len; i++) {
			sort[i] = arr[i];
		}
		Arrays.sort(sort);
		this.f(arr, sort, len - 1, ans);
		return ans;
	}

	private void f(int[] arr, int[] sort, int index, List<Integer> ans) {
		if (index == -1) {
			return;
		}
		if (arr[index] == sort[index]) {
			this.f(arr, sort, index - 1, ans);
		} else {
			// 3241 1234
			int find = sort[index];
			int[] next = new int[index];
			int p = 0;
			int end = 0;
			for (int i = index; i >= 0; i--) {
				if (arr[i] != find) {
					next[p++] = arr[i];
				} else {
					end = i;
					break;
				}
			}
			for (int i = 0; i < end; i++) {
				next[p++] = arr[i];
			}
			ans.add(end + 1);
			ans.add(index + 1);
			this.f(next, sort, index - 1, ans);
		}

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
